﻿var zombies = zombies || {};

zombies.Dashboard = function () {
    var self = this;
    self.NewestUsers = ko.observableArray();
    self.NewestAds = ko.observableArray();

    self.Hits = ko.observable();
    self.Ads = ko.observable();

    self.mems = ko.observable();
    self.pubs = ko.observable();
}

var loadStaticMemPub = function (element) {
    var time = new Date($.now());
    var year = time.getFullYear();
    var mem = [];
    var pub = [];
    var sumMem = 0;
    var sumPub = 0;
    $.ajax({
        url: '/api/Statistic/GetUserStatistic/' + year,
        type: 'GET',
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).done(function (items) {
        for (var item in items) {
            if (items.hasOwnProperty(item)) {
                mem.push([items[item].Month, items[item].Count]);
                sumMem = sumMem + parseInt(items[item].Count, 10);
                pub.push([items[item].Month, items[item].Pub]);
                sumPub = sumPub + parseInt(items[item].Pub, 10);
            }
        }
        TebbeeChart.init(mem, pub, element);
        dashboardModel.mems(sumMem);
        dashboardModel.pubs(sumPub);
    }).error(function () {
        toastr.error('Lỗi gì đó');
    })

}

var loadNewestUser = function () {
    $.ajax({
        url: '/api/Statistic/GetNewestUser',
        type: 'GET',
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).done(function (items) {
        for (var item in items) {
            if (items.hasOwnProperty(item)) {
                dashboardModel.NewestUsers.push(new zombies.User(items[item]))
            }
        }
    }).error(function () {
        toastr.error('error');
    })
}

var loadNewsestAds = function () {
    $.ajax({
        url: '/api/Statistic/GetAdvertisesStatistic',
        type: 'GET',
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).done(function (items) {
        for (var item in items) {
            if (items.hasOwnProperty(item)) {
                dashboardModel.NewestAds.push(new zombies.Advertise(items[item]))
            }
        }
    }).error(function () {
        toastr.error('error');
    })
}

var loadDashboard = function () {
    $.ajax({
        url: '/api/Statistic/GetHitsAds',
        type: 'GET',
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).done(function (item) {
        dashboardModel.Hits(item.Hits === null?0: item.Hits);
        dashboardModel.Ads(item.Ads);
    }).error(function () {
        toastr.error('error');
    })
}


var dashboardModel;
$(document).ready(function () {

    dashboardModel = new zombies.Dashboard();

    loadStaticMemPub($('#chart-staticMemPub'));
    loadNewestUser();
    loadNewsestAds();
    loadDashboard();

    ko.applyBindings(dashboardModel, document.getElementById("admin_content"));

});